package com.syp.lease.web.app.service.impl;

import com.syp.lease.model.entity.*;
import com.syp.lease.model.enums.ItemType;
import com.syp.lease.web.app.mapper.GraphInfoMapper;
import com.syp.lease.web.app.mapper.LeaseAgreementMapper;
import com.syp.lease.web.app.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syp.lease.web.app.vo.agreement.AgreementDetailVo;
import com.syp.lease.web.app.vo.agreement.AgreementItemVo;
import com.syp.lease.web.app.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 租约服务实现类
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;

    /**
     * 根据电话号码列出租约项
     *
     * @param phone 电话号码
     * @return 租约项列表
     */
    @Override
    public List<AgreementItemVo> listItemByPhone(String phone) {
        return baseMapper.listItemByPhone(phone);
    }

    /**
     * 根据ID获取租约详情
     *
     * @param id 租约ID
     * @return 租约详情对象
     */
    @Override
    public AgreementDetailVo getDetailById(Long id) {
        // 从数据库中获取租约实体
        LeaseAgreement leaseAgreement = baseMapper.selectById(id);
        if (leaseAgreement == null) {
            return null;
        }
        // 获取公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(leaseAgreement.getApartmentId());
        // 获取公寓图片信息列表
        List<GraphVo> apartmentGraphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, leaseAgreement.getApartmentId());
        // 获取房间信息
        RoomInfo roomInfo = roomInfoService.getById(leaseAgreement.getRoomId());
        // 获取房间图片信息列表
        List<GraphVo> roomGraphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, leaseAgreement.getRoomId());
        // 获取支付方式信息
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        // 获取租约期限信息
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());
        // 创建租约详情视图对象
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        // 将租约实体属性复制到视图对象
        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);
        // 设置公寓名称
        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        // 设置房间号
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        // 设置公寓图片信息列表
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphVoList);
        // 设置房间图片信息列表
        agreementDetailVo.setRoomGraphVoList(roomGraphVoList);
        // 设置支付方式
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        // 设置租约期限月数
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        // 设置租约期限单位
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());

        return agreementDetailVo;
    }
}
